package com.orange.studio.dbk.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.orange.studio.dbk.DBK_ApplicationContext;
import com.orange.studio.dbk.listeners.FavioritesIF;

public class FavioritesDAO implements FavioritesIF {

	private static FavioritesIF instance=null;
	private static final String FAVIORITES_SHAREDREFENCES="DBK_Faviorites";
	private static final String USER_LIKED_KEY="userLiked";
	private SharedPreferences mPreferences=null;
	public static FavioritesIF getInstance(){
		if(instance==null){
			instance=new FavioritesDAO();
		}
		return instance;
	}
	private FavioritesDAO(){
		mPreferences=DBK_ApplicationContext.getContext().getSharedPreferences(FAVIORITES_SHAREDREFENCES,  Context.MODE_PRIVATE);
	}	
	@Override
	public List<Integer> getListFaviorites() {
		List<Integer> result=null;
		try {
			String ids=mPreferences.getString(USER_LIKED_KEY, null);
			if(ids!=null && ids.trim().length()>0){
				result=new ArrayList<Integer>();
				String[] arrIds=ids.split(",");
				for (int i = 0; i < arrIds.length; i++) {
					result.add(Integer.valueOf(arrIds[i]));
				}
			}
		} catch (Exception e) {
			return null;
		}
		return result;
	}
	public boolean setFaviorites(List<Integer> listIds){
		if(listIds==null || listIds.size()<1){
			return false;
		}
		boolean result=true;		
		try {
			String strListIds="";
			for (int i = 0; i < listIds.size(); i++) {
				if(i==listIds.size()-1){
					strListIds+=listIds.get(i).toString();
				}else{
					strListIds+=listIds.get(i).toString()+",";
				}
			}
			Editor editor=mPreferences.edit();
			editor.putString(USER_LIKED_KEY, strListIds);
			editor.commit();
		} catch (Exception e) {
			result=false;
		}
		return result;
	}
	public boolean setFaviorites(int id){
		boolean result=true;
		try {
			
		} catch (Exception e) {
			result=false;
		}
		return result;
	}
}
